package com.mission.schedule.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.mission.schedule.CommonDialog.LocalSynServiceDialog;
import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.RepeatUpLoadBackBean;
import com.mission.schedule.bean.UpLoadBackBean;
import com.mission.schedule.clock.WriteAlarmClock;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.service.UpLoadService;
import com.mission.schedule.utils.ActivityManager1;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.SharedPrefUtil;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2016/8/22.
 */
public class ExitAlterActivity extends BaseActivity implements View.OnClickListener {

    @ViewResId(id = R.id.content_tv)
    private TextView content_tv;
    @ViewResId(id = R.id.update_bt)
    private Button update_bt;
    @ViewResId(id = R.id.exit_tv)
    private TextView exit_tv;
    @ViewResId(id = R.id.cancle_tv)
    private TextView cancle_tv;

    Context context;
    ActivityManager1 activityManager = null;
    SharedPrefUtil sharedPrefUtil = null;
    ProgressUtil progressUtil = null;
    String startContentStr = "";
    String redContentStr = "";
    String endContentStr = "";

    App application = null;
    List<Map<String, String>> upList;
    List<Map<String, String>> upRepeatList;
    String schuppath = "";
    String schjson = "";
    String repeatUpPath = "";
    String repJson = "";
    String UserId = "0";

    @Override
    protected void setListener() {
        update_bt.setOnClickListener(this);
        exit_tv.setOnClickListener(this);
        cancle_tv.setOnClickListener(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_exitalter);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        activityManager = ActivityManager1.getInstance();
        activityManager.addActivities(this);
        application = App.getDBcApplication();
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        UserId = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "0");
        startContentStr = "<font color='" + context.getResources().getColor(R.color.black) + "'>"
                + "本地还有" + "</font>";
        redContentStr = "<font color='" + context.getResources().getColor(R.color.sunday_txt) + "'>"
                + "未同步的数据" + "</font>";
        endContentStr = "<font color='" + context.getResources().getColor(R.color.black) + "'>"
                + "，如果继续切换，数据将会丢失。请您在网络畅通的环境，先上传数据，再切换账号。" + "</font>";
        content_tv.setText(Html.fromHtml(startContentStr + redContentStr + endContentStr));
    }

    @Override
    protected void setAdapter() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.update_bt:
                if(NetUtil.getConnectState(context)!= NetUtil.NetWorkState.NONE){
                    LocalSynServiceDialog localSynServiceDialog = new LocalSynServiceDialog(context, R.style.dialog_translucent, getWindowManager());
                    localSynServiceDialog.show();
                    localSynServiceDialog.setCallBack(new LocalSynServiceDialog.CallBack() {
                        @Override
                        public void dialogDismiss() {
                            clearMessage();
                        }
                    });
                }else {
                    Toast.makeText(context,"请检查您的网络!",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.exit_tv:
                clearMessage();
                break;
            case R.id.cancle_tv:
                this.finish();
                break;
        }
    }
    private void clearMessage(){
        WriteAlarmClock.clearAlarmClock(getApplicationContext());
        if (!"".equals(sharedPrefUtil.getString(context,
                ShareFile.USERFILE, ShareFile.TELEPHONE, ""))) {
            sharedPrefUtil.putString(context, ShareFile.USERFILE,
                    ShareFile.TELEPHONE, "");
        } else {
            sharedPrefUtil.putString(context, ShareFile.USERFILE,
                    ShareFile.USEREMAIL, "");
        }
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.QIANDAOSTRING, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISADMIN, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERID, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERBACKGROUNDPATH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERPHOTOPATH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.USERSTATE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FIRSTLOGIN, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.U_ACC_NO, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.MAXFOCUSID, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.OUTWEEKFAG, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UPDATESETTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UPDATENOTESETTIME, "2017-01-01%2B00:00:00");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNSCHTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNREPTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.RINGSTATE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISYOUKE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNTAGDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.OPENSTYLESTATE, "2");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNSCHTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNRepTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FRIENDDOWNOldTIME, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PUSH_ALIAS, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.REFRESHFRIEND, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.FIRSTDOWNFOCUSSCH, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.SHOUCANGDATA, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHARESCHDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DOWNNEWFOCUSSHAREREPDATE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.KuaiJieSouSuo, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.SHAREDATA, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.LOCALPATH, "1");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISMOSTIMAGE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ISSJMOSTIMAGE, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE1, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE2, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE3, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE4, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE5, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE6, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.CUSTIOMMOSTIMAGE7, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.DQRBEFORETIME, "4320");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.ZJF, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.UNTASKEND, "1");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PERSONREMARK, "");
        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                ShareFile.PERSONDOWNTIME, "2016-01-01%2B00:00:00");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISMOSTIMAGE, "0");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.MORNINGTIME, "07:58");
        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.NIGHTTIME, "20:58");
        ClearMAC();
        Intent newt2 = new Intent(context,PhoneCodeLoginNewActivity.class);
        newt2.putExtra("type", 0);
        newt2.putExtra("login",true);
        context.startActivity(newt2);
        activityManager.doAllActivityFinish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    /**
     * 修改推送mac地址置为空
     */
    private void ClearMAC() {
        if (NetUtil.getConnectState(context) != NetUtil.NetWorkState.NONE) {
            if (sharedPrefUtil.getString(context,
                    ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                final String path = URLConstants.修改MAC地址 + UserId + "&uClintAddr="
                        + "" + "&uTocode=android";
                StringRequest request = new StringRequest(Request.Method.GET, path,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String s) {

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                });
                request.setTag("down");
                request.setRetryPolicy(new DefaultRetryPolicy(20000, 1, 1.0f));
                App.getHttpQueues().add(request);
            }
        } else {
            return;
        }
    }
}
